package com.suyi.service.impl;

import com.suyi.dao.TOrderDao;
import com.suyi.domain.TOrder;
import com.suyi.service.TAccountService;
import com.suyi.service.TOrderService;
import com.suyi.service.TStorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 贺袁晨
 * 下订单--减库存---减余额----改状态
 * @since 2021-04-26
 */
@Service
@Slf4j
public class TOrderServiceImpl implements TOrderService {

    @Autowired
    private TOrderDao orderDao;
    @Autowired
    private TStorageService storageService;
    @Autowired
    private TAccountService accountService;
    @Override
//    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(TOrder order) {
        //1.下订单
        log.info("-------------->开始新建订单");
        orderDao.create(order);

        //2.减库存
        log.info("--------------->订单微服务开始调用库存，做扣减 Count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("--------------->订单微服务开始调用库存，做扣减 end");

        //3.减余额
        log.info("--------------->订单微服务开始调用账户，做扣减 Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("--------------->订单微服务开始调用账户，做扣减 end");

        //4.修改订单状态，从0到1  1 代表已经完成
        log.info("-------------->修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("-------------->修改订单状态结束");

        log.info("-------------->开始新建订单");
        log.info("-------------->下订单结束");

    }

    @Override
    public void update(Long userId, Integer status) {
        orderDao.update(userId,status);
    }
}
